package com.woniuxy.business.controller;

import com.woniuxy.business.service.impl.BusinessInfoServiceImpl;
import com.woniuxy.business.service.impl.PreLoanServiceImpl;
import com.woniuxy.entity.BusinessInfo;
import com.woniuxy.entity.PreLoan;
import com.woniuxy.utils.ResponseResult;
import com.woniuxy.utils.ResultStatus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/proLoan")
public class PreLoanController {
    @Resource
    private PreLoanServiceImpl preLoanService;
    @Resource
    private BusinessInfoServiceImpl businessInfoService;

    @PostMapping("/add")
    public ResponseResult add(@RequestBody PreLoan preLoan){
        ResponseResult responseResult = new ResponseResult();
        if(preLoan != null){
            preLoanService.addPreLoan(preLoan);
            responseResult.setCode(200);
            responseResult.setStatus(ResultStatus.REQUEST_SUCCESS);
            responseResult.setMessage("添加成功");
            responseResult.setData(null);
        }else{
            responseResult.setCode(230);
            responseResult.setStatus(ResultStatus.REQUEST_FAILED);
            responseResult.setMessage("添加失败");
            responseResult.setData(null);
        }
        return responseResult;
    }
    @GetMapping("/get")
    public ResponseResult<List<PreLoan>> get(String business_name,
                    String process_status, int pageIndex, int pageSize){
        ResponseResult<List<PreLoan>> responseResult = new ResponseResult<>(200,ResultStatus.REQUEST_SUCCESS,"查询成功",null);
        if(business_name != null){
            BusinessInfo businessInfo = businessInfoService.getBusinessInfoByBusinessName(business_name);
            List<PreLoan> preLoanByBusinessName = preLoanService.getPreLoanByBusinessName(businessInfo.getId());
            responseResult.setData(preLoanByBusinessName);
        }else if(process_status != null){
            List<PreLoan> preLoanByStatus = preLoanService.getPreLoanByStatus(process_status, pageIndex, pageSize);
            responseResult.setData(preLoanByStatus);
        }
        return responseResult;
    }
    @DeleteMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable("id")int id){
        preLoanService.removePreLoan(id);
        ResponseResult responseResult = new ResponseResult(200,ResultStatus.REQUEST_SUCCESS,"删除成功",null);
        return responseResult;
    }
}
